package com.bdqn.controller;

import com.bdqn.LayUI.Layui;
import com.bdqn.Service.IdMapping.IdMappingService;
import com.bdqn.Service.Orders.OrdersService;
import com.bdqn.Service.QuoteDetail.QuoteDetailSevice;
import com.bdqn.Service.Stock.StockService;
import com.bdqn.entity.Employee;
import com.bdqn.entity.Orders;
import com.bdqn.entity.QuoteDetail;
import com.bdqn.entity.Stock;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/stock")
public class StockController {
    @Autowired
    private StockService service;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private IdMappingService idMappingService;
    @Autowired
    private QuoteDetailSevice quoteDetailSevice;
    /**
     * 分页查询和按条件查询
     * @param page
     * @param limit
     * @param stock
     * @return
     */
    @RequestMapping("/")
    @ResponseBody
    public Layui<Stock> findAllByPage(Integer page, Integer limit, Stock stock){
        page = (page-1)*limit;
        List<Stock> stocks = service.findAllByPageAndLike(stock,page,limit);
        int count = service.getCount();
        Layui<Stock> stockLayui=new Layui<Stock>();
        stockLayui.setCode(0);
        stockLayui.setData(stocks);
        stockLayui.setCount(count);
        return stockLayui;
    }
    /**
     * 分页查询和按条件查询待审信息
     * @param page
     * @param limit
     * @param stock
     * @return
     */
    @RequestMapping("/findDs")
    @ResponseBody
    public Layui<Stock> findDs(Integer page, Integer limit, Stock stock){
        page = (page-1)*limit;
        List<Stock> stocks = service.findSP(stock,page,limit);
        int count = service.getSPCount();
        Layui<Stock> stockLayui=new Layui<Stock>();
        stockLayui.setCode(0);
        stockLayui.setData(stocks);
        stockLayui.setCount(count);
        return stockLayui;
    }
    /**
     * 编制询价书
     * @return
     */
    public String updateStock(){
        return "";
    }

    /**
     * 编制采购计划
     */
    @RequestMapping("/Insert")
    @ResponseBody
    public Stock Insert(@RequestParam(value = "ids[]",required = false) int[] ids){
        ordersService.UpdateIds(ids);
        Stock stock=new Stock();
        stock.setSsStatic("未确定");
        Calendar calendar=Calendar.getInstance();
        stock.setStockNum("1000"+calendar.get(Calendar.YEAR)+calendar.get(Calendar.MONTH)+calendar.get(Calendar.DATE)+calendar.get(Calendar.HOUR_OF_DAY)+calendar.get(Calendar.MINUTE)+calendar.get(Calendar.SECOND));
        service.Insert(stock);
        service.Stork_OrderInsert(service.findById(stock).getId(),ids);
        Stock stock1 = service.FindByNum(stock);
        List<Orders> orders = stock1.getOrders();
        double num=0;
        for (Orders order : orders) {
            num+=order.getSumPrice();
        }
        stock1.setBudget(num);
        return stock1;
    }

    /**
     * 修改
     */
    @RequestMapping("/Update")
    @ResponseBody
    public Stock Update(int id){
        Stock stock=new Stock();
        stock.setId(id);
        Stock stock1 = service.FindByNum1(stock);
        return stock1;
    }
    /**
     * 编制采购计划 确定
     */
    @RequestMapping("/QueDing")
    @ResponseBody
    public boolean Update(Stock stock,@RequestParam(value = "iidds[]",required = false) int[] iidds,@RequestParam(value = "gysId[]",required = false) int[] gysId){
        if(gysId!=null){
            for (int i : gysId) {
                for (int iidd : iidds) {
                    QuoteDetail quoteDetail=new QuoteDetail();
                    quoteDetail.setOrderId(iidd);
                    quoteDetail.setSupplierId(i);
                    quoteDetail.setStatus("未报价");
                    quoteDetail.setEnquireId(stock.getStockNum());
                    quoteDetailSevice.Insert(quoteDetail);
                }
            }
        }

        Subject subject= SecurityUtils.getSubject();
        Session session = subject.getSession();
        Employee employee = (Employee) session.getAttribute("employee");
        stock.setAuthorId(employee.getEmpNum());
        stock.setAuthor(employee.getEmpName());
        return service.Update(stock);
    }

    /**
     * 采购计划查询
     */
    @RequestMapping("/SelX")
    @ResponseBody
    public Layui<Stock> SelX(Integer page, Integer limit, Stock stock){
        page = (page-1)*limit;
        List<Stock> stocks = service.SelX(stock,page,limit);
        int count = service.getCount();
        Layui<Stock> stockLayui=new Layui<Stock>();
        stockLayui.setCode(0);
        stockLayui.setData(stocks);
        stockLayui.setCount(count);
        return stockLayui;
    }

    /**
     * 采购计划查询
     */
    @RequestMapping("/SelX1")
    @ResponseBody
    public Layui<Stock> SelX1(Integer page, Integer limit, Stock stock){
        stock.setSsStatic("未下达");
        page = (page-1)*limit;
        List<Stock> stocks = service.SelX(stock,page,limit);
        int count = service.getCount();
        Layui<Stock> stockLayui=new Layui<Stock>();
        stockLayui.setCode(0);
        stockLayui.setData(stocks);
        stockLayui.setCount(count);
        return stockLayui;
    }

    /**
     * 采购计划查询
     */
    @RequestMapping("/SelX2")
    @ResponseBody
    public Layui<Stock> SelX2(Integer page, Integer limit, Stock stock){
        page = (page-1)*limit;
        List<Stock> stocks = service.SelX1(stock,page,limit);
        int count = service.getCount();
        Layui<Stock> stockLayui=new Layui<Stock>();
        stockLayui.setCode(0);
        stockLayui.setData(stocks);
        stockLayui.setCount(count);
        return stockLayui;
    }

    /**
     * 报批
     */
    @RequestMapping("/BaoPi")
    @ResponseBody
    public boolean BaoPi(Stock stock){
        stock.setSsStatic("待审");
        return service.UpdateById(stock);
    }

    /**
     * 报批
     */
    @RequestMapping("/XiaDa")
    @ResponseBody
    public boolean XiaDa(Stock stock){
        stock.setSsStatic("下达");
        stock.setSubmitDate(new Date());
        return service.UpdateById(stock);
    }
    /**
     * 详情
     */
    @RequestMapping("/XiangQing")
    @ResponseBody
    public Stock XiangQing(Stock s){
        return service.FindByNum(s);
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @ResponseBody
    public boolean del(int id){
        idMappingService.deleteByStId(id);
        return service.Delete(id);
    }

    @RequestMapping("/UpdateByIdPr")
    @ResponseBody
    public boolean UpdateById(Stock stock){
        Subject subject= SecurityUtils.getSubject();
        Session session = subject.getSession();
        Employee employee = (Employee) session.getAttribute("employee");
        stock.setLeader(employee.getEmpName());
        boolean stock1 = service.UpdateByIdPr(stock);
        return stock1;
    }
}
